package com.moody.hibernate.dao;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.IndInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * IndInfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.IndInfo
 * @author MyEclipse Persistence Tools
 */

public class IndInfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(IndInfoDAO.class);
	// property constants
	public static final String IDTYPE = "idtype";
	public static final String IDNO = "idno";
	public static final String CUST_NAME = "custName";
	public static final String SEX_TYPE = "sexType";
	public static final String BIRTH_DATE = "birthDate";
	public static final String GRADE = "grade";
	public static final String DEGREE = "degree";
	public static final String ZIP_ADD = "zipAdd";
	public static final String ZIPCODE = "zipcode";
	public static final String HOUSE_ADD = "houseAdd";
	public static final String WORKTEL = "worktel";
	public static final String CELL_PHONE_NO = "cellPhoneNo";
	public static final String EMAIL_ADD = "emailAdd";
	public static final String HR_ADD = "hrAdd";
	public static final String MSTATUS = "mstatus";
	public static final String MATE_NAME = "mateName";
	public static final String MATEWORK = "matework";
	public static final String MATETEL = "matetel";
	public static final String RESIDE_ADD = "resideAdd";
	public static final String RESIDE_STATE = "resideState";
	public static final String RESINFO_DATE = "resinfoDate";
	public static final String CO_NAME = "coName";
	public static final String CO_ADD = "coAdd";
	public static final String CO_ZIP_CODE = "coZipCode";
	public static final String INDUSTRY = "industry";
	public static final String JOB_TYPE = "jobType";
	public static final String JOB_POSITION = "jobPosition";
	public static final String JOB_TITLE = "jobTitle";
	public static final String YEAR_INCOME = "yearIncome";
	public static final String START_YEAR = "startYear";
	public static final String CO_INFO_DATE = "coInfoDate";
	public static final String MODEL_ID = "modelId";
	public static final String CREDIT_LEVEL = "creditLevel";
	public static final String CREDIT_SCORE = "creditScore";
	public static final String CREDIT_DATE = "creditDate";
	public static final String CREDIT_USER_ID = "creditUserId";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";
	public static final String UPDATE_USER_ID = "updateUserId";
	public static final String UPDATE_ORG_ID = "updateOrgId";
	public static final String UPDATE_DATE = "updateDate";

	protected void initDao() {
		// do nothing
	}

	public void save(IndInfo transientInstance) {
		log.debug("saving IndInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(IndInfo persistentInstance) {
		log.debug("deleting IndInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public IndInfo findById(java.lang.String id) {
		log.debug("getting IndInfo instance with id: " + id);
		try {
			IndInfo instance = (IndInfo) getHibernateTemplate().get(
					"com.moody.hibernate.domain.IndInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(IndInfo instance) {
		log.debug("finding IndInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding IndInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from IndInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByIdtype(Object idtype) {
		return findByProperty(IDTYPE, idtype);
	}

	public List findByIdno(Object idno) {
		return findByProperty(IDNO, idno);
	}

	public List findByCustName(Object custName) {
		return findByProperty(CUST_NAME, custName);
	}

	public List findBySexType(Object sexType) {
		return findByProperty(SEX_TYPE, sexType);
	}

	public List findByBirthDate(Object birthDate) {
		return findByProperty(BIRTH_DATE, birthDate);
	}

	public List findByGrade(Object grade) {
		return findByProperty(GRADE, grade);
	}

	public List findByDegree(Object degree) {
		return findByProperty(DEGREE, degree);
	}

	public List findByZipAdd(Object zipAdd) {
		return findByProperty(ZIP_ADD, zipAdd);
	}

	public List findByZipcode(Object zipcode) {
		return findByProperty(ZIPCODE, zipcode);
	}

	public List findByHouseAdd(Object houseAdd) {
		return findByProperty(HOUSE_ADD, houseAdd);
	}

	public List findByWorktel(Object worktel) {
		return findByProperty(WORKTEL, worktel);
	}

	public List findByCellPhoneNo(Object cellPhoneNo) {
		return findByProperty(CELL_PHONE_NO, cellPhoneNo);
	}

	public List findByEmailAdd(Object emailAdd) {
		return findByProperty(EMAIL_ADD, emailAdd);
	}

	public List findByHrAdd(Object hrAdd) {
		return findByProperty(HR_ADD, hrAdd);
	}

	public List findByMstatus(Object mstatus) {
		return findByProperty(MSTATUS, mstatus);
	}

	public List findByMateName(Object mateName) {
		return findByProperty(MATE_NAME, mateName);
	}

	public List findByMatework(Object matework) {
		return findByProperty(MATEWORK, matework);
	}

	public List findByMatetel(Object matetel) {
		return findByProperty(MATETEL, matetel);
	}

	public List findByResideAdd(Object resideAdd) {
		return findByProperty(RESIDE_ADD, resideAdd);
	}

	public List findByResideState(Object resideState) {
		return findByProperty(RESIDE_STATE, resideState);
	}

	public List findByResinfoDate(Object resinfoDate) {
		return findByProperty(RESINFO_DATE, resinfoDate);
	}

	public List findByCoName(Object coName) {
		return findByProperty(CO_NAME, coName);
	}

	public List findByCoAdd(Object coAdd) {
		return findByProperty(CO_ADD, coAdd);
	}

	public List findByCoZipCode(Object coZipCode) {
		return findByProperty(CO_ZIP_CODE, coZipCode);
	}

	public List findByIndustry(Object industry) {
		return findByProperty(INDUSTRY, industry);
	}

	public List findByJobType(Object jobType) {
		return findByProperty(JOB_TYPE, jobType);
	}

	public List findByJobPosition(Object jobPosition) {
		return findByProperty(JOB_POSITION, jobPosition);
	}

	public List findByJobTitle(Object jobTitle) {
		return findByProperty(JOB_TITLE, jobTitle);
	}

	public List findByYearIncome(Object yearIncome) {
		return findByProperty(YEAR_INCOME, yearIncome);
	}

	public List findByStartYear(Object startYear) {
		return findByProperty(START_YEAR, startYear);
	}

	public List findByCoInfoDate(Object coInfoDate) {
		return findByProperty(CO_INFO_DATE, coInfoDate);
	}

	public List findByModelId(Object modelId) {
		return findByProperty(MODEL_ID, modelId);
	}

	public List findByCreditLevel(Object creditLevel) {
		return findByProperty(CREDIT_LEVEL, creditLevel);
	}

	public List findByCreditScore(Object creditScore) {
		return findByProperty(CREDIT_SCORE, creditScore);
	}

	public List findByCreditDate(Object creditDate) {
		return findByProperty(CREDIT_DATE, creditDate);
	}

	public List findByCreditUserId(Object creditUserId) {
		return findByProperty(CREDIT_USER_ID, creditUserId);
	}

	public List findByInputUserId(Object inputUserId) {
		return findByProperty(INPUT_USER_ID, inputUserId);
	}

	public List findByInputOrgId(Object inputOrgId) {
		return findByProperty(INPUT_ORG_ID, inputOrgId);
	}

	public List findByInputDate(Object inputDate) {
		return findByProperty(INPUT_DATE, inputDate);
	}

	public List findByUpdateUserId(Object updateUserId) {
		return findByProperty(UPDATE_USER_ID, updateUserId);
	}

	public List findByUpdateOrgId(Object updateOrgId) {
		return findByProperty(UPDATE_ORG_ID, updateOrgId);
	}

	public List findByUpdateDate(Object updateDate) {
		return findByProperty(UPDATE_DATE, updateDate);
	}

	public List findAll() {
		log.debug("finding all IndInfo instances");
		try {
			String queryString = "from IndInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public IndInfo merge(IndInfo detachedInstance) {
		log.debug("merging IndInfo instance");
		try {
			IndInfo result = (IndInfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(IndInfo instance) {
		log.debug("attaching dirty IndInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(IndInfo instance) {
		log.debug("attaching clean IndInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IndInfoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IndInfoDAO) ctx.getBean("IndInfoDAO");
	}

	public void update(IndInfo custInfo) {
		log.debug("saving CustInfo instance");
		try {
			getHibernateTemplate().update(custInfo);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public List<IndInfo> findByIdAndName(String condition, String keyword) {
		String hql = "from IndInfo where " + condition + " like :queryString";
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		query.setString("queryString", "%"+keyword+"%");
		
		List<IndInfo> list = query.list();
		session.close();
		return list;
	}

	public List<IndInfo> findIndInfo(String userId, List<String> subOrgIdList) {
		if(subOrgIdList == null || subOrgIdList.size() == 0){
			String hql = "FROM IndInfo WHERE  inputUserId like :userId";
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			Query query = session.createQuery(hql);
			query.setString("userId", userId);
			List<IndInfo> list = query.list();
			session.close();
			return list;
		}
		else{
			String orgIdArray = "";
			for(int i = 0; i < subOrgIdList.size();i++){
				orgIdArray += "?,";
			}
			orgIdArray = orgIdArray.substring(0, orgIdArray.length() - 1);
			String hql = "FROM IndInfo WHERE (inputUserId = ? OR inputOrgId IN ("+orgIdArray+"))";
			Session session = getHibernateTemplate().getSessionFactory().openSession();
			Query query = session.createQuery(hql);
			
			query.setString(0, userId);
			for(int i = 0; i < subOrgIdList.size(); i++){
				query.setString(i+1, subOrgIdList.get(i));
			}
			List<IndInfo> list = query.list();
			session.close();
			return list;
		}
	}

	public List<IndInfo> findCustByCondition(Map<String, String> condition) {
		String hql = "FROM IndInfo WHERE ";
		Set<String> keySet = condition.keySet();
		Iterator<String> it = keySet.iterator();
		while(it.hasNext()){
			String keyString = it.next();
			hql += keyString + " like :" + keyString + " AND ";
		}
		hql = hql.substring(0,hql.length() - 5);
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createQuery(hql);
		
		it = keySet.iterator();
		while(it.hasNext()){
			String keyString = it.next();
			query.setString(keyString, "%" + condition.get(keyString) +"%");
		}
		List<IndInfo> list = query.list();
		session.close();
		return list;
	}
}